package com.dfrz.dao;

import com.dfrz.entry.GoodsInfo;
import com.dfrz.entry.ShoppingCar;
import com.dfrz.util.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class ShoppingCarDao {
    public int insertShoppingCar(ShoppingCar shoppingCar){
        int count=0;
        String sqlStr="insert into shoppingcar(number,goodsid,goodsname,singlePrice,totalPrice,imgURL,userid) values(?,?,?,?,?,?,?)";
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        try {
            count=queryRunner.update(sqlStr,shoppingCar.getNumber(),shoppingCar.getGoodsid(),shoppingCar.getGoodsname(),shoppingCar.getSinglePrice(),shoppingCar.getTotalprice(),shoppingCar.getImgURL(),shoppingCar.getUserid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;

    }
    //根据货物id和用户id查询出该用户的某个商品的购物车
    public ShoppingCar queyrcCarById(int goodsid,int userid){
        ShoppingCar car = new ShoppingCar();
        String sqlStr="select * from shoppingcar where goodsid=? and userid=? and status=1";
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        try {
            car=queryRunner.query(sqlStr,new BeanHandler<ShoppingCar>(ShoppingCar.class),goodsid,userid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return car;

    }
    //删除购物车中的某个商品
    public int deleteShoppingCar(ShoppingCar shoppingCar){
        String sqlStr="update shoppingcar set status=-1 where goodsid=? and userid=?";
        int count=0;
        QueryRunner queryRunner=new QueryRunner(JdbcUtils.getDataSource());
        try {
            count=queryRunner.update(sqlStr,shoppingCar.getGoodsid(),shoppingCar.getUserid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }
    //更新该用户的某个商品的数量和总价
    public int update_goodsnum(ShoppingCar car){
        int count=0;
        if(car.getGoodsid()==0||car.getUserid()==0){
            return count;
        }
        String  sqlStr="update shoppingcar set number=?,totalprice=? where goodsid=? and userid=? and status=1";
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        try {
            count=queryRunner.update(sqlStr,car.getNumber(),car.getTotalprice(),car.getGoodsid(),car.getUserid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }
    //查询该用用户多有的购物车商品
    public List<ShoppingCar> querySomeoneCars(int userid){
        List<ShoppingCar> list=null;
        String sqlStr="select * from shoppingcar where userid=? and status=1";
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        try {
            list=queryRunner.query(sqlStr,new BeanListHandler<ShoppingCar>(ShoppingCar.class),userid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
